package com.travelDevelop.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.travelDevelop.dao.PermissionMapper;


@Service
public class PermissionService {

	@Autowired
	public PermissionMapper permissionMapper;

	public boolean hasPermission(String userId, String[] operationNames) {
		if (operationNames == null ||operationNames.length==0) {
			return true;
		}
		int count = permissionMapper.permissionsCount(userId, operationNames);
		if (count != operationNames.length) {
			return false;
		}
		return true;
	}
	
	public boolean hasPermission(String userId, Set<String> set) {
		if (set == null || set.size()==0) {
			return true;
		}
		String[] operationNames = new String[set.size()];
		int count = 0;
		for(String operationName:set){
			operationNames[count] = operationName;
			count++;
		}
		return hasPermission(userId,operationNames);
	}
	
	public boolean hasPermission(String userId, String operationName){
		if (operationName == null) {
			return true;
		}
		int count = permissionMapper.permissionCount(userId, operationName);
		if (count != 1) {
			return false;
		}
		return true;
	}
}
